From f0ae3c4c7b4b33258cb797120892646342add9b7 Mon Sep 17 00:00:00 2001 From: Mukund Sivaraman Date: Wed, 22 Apr 2015 13:40:06 +0530 Subject: [PATCH] Don't create a mutexattr for each mutex --- babl/babl-mutex.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/babl/babl-mutex.c b/babl/babl-mutex.c index 708c54a..4fa5f29 100644 --- a/babl/babl-mutex.c +++ b/babl/babl-mutex.c @@ -21,6 +21,28 @@ #include +#ifndef _WIN32 + +static const pthread_mutexattr_t * +get_mutex_attr (void) +{ + static pthread_mutexattr_t mutexattr; + static int initialized = 0; + + if (!initialized) + { + /* On some platforms, this will keep an allocation till process + termination, but it isn't a growing leak. */ + pthread_mutexattr_init (&mutexattr); + pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_RECURSIVE); + initialized = 1; + } + + return &mutexattr; +} + +#endif + BablMutex * babl_mutex_new (void) { @@ -28,12 +50,7 @@ babl_mutex_new (void) #ifdef _WIN32 InitializeCriticalSection (mutex); #else - pthread_mutexattr_t mutexattr; - - pthread_mutexattr_init (&mutexattr); - pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init (mutex, &mutexattr); - pthread_mutexattr_destroy (&mutexattr); + pthread_mutex_init (mutex, get_mutex_attr ()); #endif return mutex; } -- 2.30.2